package com.example.game_level;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.CameraScene;

import org.andengine.entity.scene.background.Background;
import org.andengine.entity.text.Text;


import android.util.Log;



public class EndScene extends CameraScene {

	StartActivity activity;
	

	public EndScene(){
		
		super(StartActivity.getSharedInstance().mCamera);
	    activity=StartActivity.getSharedInstance();
		
		activity.getEngine().registerUpdateHandler(new TimerHandler(3,true, new ITimerCallback()		
 	 	
        {                      
            @Override
            public void onTimePassed(final TimerHandler pTimerHandler)
            {
            	    Log.v("The_game", "gameScene_timer");
            	    
            	    activity.onBackPressed();
            	   
            	    if(activity.level>3){
            	    	activity.setCurrentScene(new MainMenuScene());
            	    }
            	    else
            	    if(activity.score>10000&&activity.level<4){
            	    	activity.level=activity.level+1;
            	    	activity.setCurrentScene(new NextScene());
            	    }
            	    else
            	    	activity.setCurrentScene(new NextScene());
            	    
 /*       	      	else
            	    activity.setCurrentScene(new MainMenuScene());
   */         	    
            	    activity.getEngine().unregisterUpdateHandler(pTimerHandler);
            	   
            	
            }
            
        }));
		 
		
	    
	    activity.bgmusic.pause();
        activity.bgsound.pause();
    	
	    setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
//	    activity.bgmusic.stop();
	    
	    if(activity.score>10000){
	    Text display=new Text(mCamera.getWidth() / 2-200 , mCamera.getHeight() / 2-100, activity.mFont, "Level Cleared!", activity.getVertexBufferObjectManager());
	    attachChild(display);
	    }
	    else{
	    	Text display=new Text(mCamera.getWidth() / 2-100 , mCamera.getHeight() / 2-100, activity.nFont, "Game Over!", activity.getVertexBufferObjectManager());
		    attachChild(display);
	    	}
	    
	    int temp=activity.score;
		int num,position=(int) (mCamera.getWidth() / 2+80) ;
		int i;
		String number[]=new String[8];
	    for(i=0;temp>0;i++){
			num=temp%10;
			temp=temp/10;
			switch(num){
			
			case 0: number[i]="0"; break;
			case 1: number[i]="1"; break;
			case 2: number[i]="2"; break;
			case 3: number[i]="3"; break;
			case 4: number[i]="4"; break;
			case 5: number[i]="5"; break;
			case 6: number[i]="6"; break;
			case 7: number[i]="7"; break;
			case 8: number[i]="8"; break;
			case 9: number[i]="9"; break;
			}
			
		}
		
		
			
	    Text score=new Text(mCamera.getWidth() / 2-100 , mCamera.getHeight() / 2-30, activity.mFont, "Score: ", activity.getVertexBufferObjectManager());
	    attachChild(score);
		
			
			
			for(i=i-1;i>=0;i--){
				
				Text num_score= new Text(3, 3, activity.mFont, number[i]  , activity.getVertexBufferObjectManager());
				
				num_score.setPosition(position,mCamera.getHeight() / 2-30);
				position+=30;
				attachChild(num_score);
			}
			if (activity.score<=0){
                Text num_score= new Text(3, 3, activity.mFont, "0"  , activity.getVertexBufferObjectManager());
                num_score.setPosition(position,mCamera.getHeight() / 2-30);
				attachChild(num_score);
			}
	    
	}

}
